<template>
  <div class="app-container">
    <el-table
      v-loading="loading"
      :data="assetList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="老师名称" align="center" prop="name" />
      <!-- <el-table-column label="状态" align="center" prop="status" /> -->
      <el-table-column label="发布人名称" align="center" prop="createBy" />
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="完成情况" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.completed + "/" + scope.row.count }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="220"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 盘点详情对话框 -->
    <el-dialog  :close-on-click-modal="false" :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-table v-loading="loading" :data="receiverList">
        <el-table-column label="设备名称" align="center" prop="deviceName" />
        <el-table-column
          label="领用人名称"
          align="center"
          prop="recipientName"
        />
        <el-table-column label="使用状态" align="center" prop="status">
          <template slot-scope="scope">
            <span v-if="scope.row.status == 0"> 使用 </span>
            <span v-else-if="scope.row.status == 1"> 使用 </span>
            <span v-else>损坏</span>
          </template>
        </el-table-column>
        <el-table-column label="盘点状态" align="center" prop="isInventory">
          <template slot-scope="scope">
            <span v-if="scope.row.isInventory == 0"> 未盘点 </span>
            <span v-else>已盘点</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="220"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              v-if="scope.row.status == 3"
              type="text"
              icon="el-icon-edit"
              @click="handlesStatus(scope.row)"
              >报修</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="infoTotal > 0"
        :total="infoTotal"
        :page.sync="queryParamsInfo.pageNum"
        :limit.sync="queryParamsInfo.pageSize"
        @pagination="queryInventoryDetailFun"
      />
      <!-- <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div> -->
    </el-dialog>
    <!-- 报修对话框 -->
    <el-dialog  :close-on-click-modal="false"
      title="报修详情"
      :visible.sync="openStatus"
      width="60%"
      append-to-body
    >
      <el-form
        :model="statusParams"
        ref="queryForm"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="资产编码">
          <el-input v-model="statusParams.assetCode"></el-input>
        </el-form-item>
        <el-form-item label="报修原因">
          <el-input type="textarea" v-model="statusParams.repair"></el-input>
        </el-form-item>
        <el-form-item label="报修人">
          <el-input v-model="statusParams.repairBy"></el-input>
        </el-form-item>
        <el-form-item label="报修时间" prop="repairTime">
          <el-date-picker
            clearable
            size="small"
            v-model="statusParams.repairTime"
            type="date"
            value-format="yyyy-MM-dd "
            placeholder="选择报修时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发布状态">
          <el-select v-model="statusParams.status" placeholder="请选择发布状态">
            <el-option label="待处理" value="0"></el-option>
            <el-option label="已处理" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitStatusForm">确 定</el-button>
        <el-button @click="cancelStatus">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryInventoryRecord,
  queryInventoryDetail,
  repairManagementInsert
} from "@/api/inventoryManagement/inventoryRecords";

import { listCategory } from "@/api/oa/category";
import { getToken } from "@/utils/auth";
import VueQr from "vue-qr";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Asset",
  components: {
    Treeselect,
    VueQr,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资产信息表格数据
      assetList: [],
      // 资产分类表格数据
      categoryOptions: [],
      // 状态字典
      statusOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        title: null,
        cateId: null,
        buyTime: null,
        buyPrice: null,
        currentPrice: null,
        masterUser: null,
      },
      // 表单参数
      form: {},
      // 日期范围
      dateRange: [],
      // 表单校验
      rules: {
        code: [{ required: true, message: "编码不能为空", trigger: "blur" }],
        title: [
          { required: true, message: "资产名称不能为空", trigger: "blur" },
        ],
        cateId: [
          { required: true, message: "分类ID不能为空", trigger: "blur" },
        ],
        masterUser: [
          { required: true, message: "负责人不能为空", trigger: "blur" },
        ],
      },
      // 查看列表
      receiverList: [],
      infoTotal: 0,
      queryParamsInfo: {
        pageNum: 1,
        pageSize: 10,
      },
      receiverId: "",
      schoolCode: "",
      openStatus: false,
      statusParams: {
        assetCode: '',
        repair: '',
        repairBy: '',
        repairTime: '',
        status: '',

      }
    };
  },
  created() {
    this.getList();
    this.getDicts("asset_status").then((response) => {
      this.statusOptions = response.data;
    });
    listCategory().then((response) => {
      this.categoryOptions = [];
      const data = { id: 0, title: "顶级节点", children: [] };
      data.children = this.handleTree(response.data, "id", "pid");
      this.categoryOptions.push(data);
    });
  },
  methods: {
    /** 查询盘点列表 */
    getList() {
      this.loading = true;
      // queryInventoryRecord({ userId: this.$store.getters.userId}).then(res => {
      queryInventoryRecord({ userId: 57 }).then((res) => {
        console.log("queryInventoryRecord", res);
        this.assetList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    /** 转换资产分类数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.title,
        children: node.children,
      };
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelStatus() {
        this.openStatus = false
    },
    submitStatusForm() {
        repairManagementInsert(this.statusParams).then(res => {
            if(res.code == 200) {
                this.$message.info('修改成功')
            }
        })
    },
    // 详情查看
    handlesStatus(row) {
        this.openStatus = true
        this.statusParams.assetCode = row.code
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        title: null,
        cateId: null,
        buyTime: null,
        buyPrice: null,
        currentPrice: null,
        masterUser: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        status: "1",
      };
      (this.receiverId = ""), (this.schoolCode = "");
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 取消按钮
    cancelQr() {
      this.qr.open = false;
      this.reset();
    },
    // /** 新增按钮操作 */
    // handleAdd() {
    //     this.reset();
    //     this.open = true;
    //     this.title = "添加资产信息";
    // },
    /** 查看按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.receiverId = row.id;
      this.schoolCode = row.schoolCode;
      this.queryInventoryDetailFun();
    },
    queryInventoryDetailFun() {
      this.loading = true;
      queryInventoryDetail({
        parentingTaskId: this.receiverId,
        schoolCode: this.schoolCode,
      }).then((res) => {
        console.log(res);
        this.open = true;
        this.receiverList = res.rows;
        this.infoTotal = res.total;
        this.title = "查看盘点信息";
        this.loading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除资产信息编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delAsset(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
  },
};
</script>
